package edu.shzu.manage.dao;

import edu.shzu.manage.entity.User;
import edu.shzu.manage.vo.UserRole;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * @mapper的作用是可以给mapper接口自动生成一个实现类，
 * 让spring对mapper接口的bean进行管理，并且可以省略去写复杂的xml文件
 *
 * 使用@mapper后，不需要在spring配置中设置扫描地址，通过mapper.xml里面的
 * namespace属性对应相关的mapper类，spring将动态的生成Bean后注入到ServiceImpl中
 */
@Mapper
public interface UserDao {
    /**
     * @Select  @Update @Insert @Delete  sql操作
     * @param user
     * @return
     */
    @Select("select u.*,r.roleId,r.roleName from user u, role r,user_role ur where u.userId = ur.userId and r.roleId = ur.roleId and u.userName = #{userName} and u.password = #{password}")
    UserRole checkUser(User user);

    @Select("select * from user where userId=#{userId}")
    User findUserById(Integer userId);

    /**
     * 根据ID 查询用户
     * @param userId
     * @return
     */
    @Select("select u.*,r.roleId,r.roleName from user u, role r,user_role ur where u.userId = ur.userId and r.roleId = ur.roleId and u.userId=#{userId}")
    UserRole getUserById(Integer userId);

    /**
     * 分页查询
     * @return
     */
    @Select("select u.*,r.roleId,r.roleName from user u, role r,user_role ur where u.userId = ur.userId and r.roleId = ur.roleId limit #{pageNum},#{pageSize}")
    List<UserRole> getAllUser(Integer pageNum,Integer pageSize);

    /**
     *查询所有用户
     * @return
     */
    @Select("select u.*,r.roleId,r.roleName from user u, role r,user_role ur where u.userId = ur.userId and r.roleId = ur.roleId")
    List<UserRole> getAllUsers();
    /**
     * 添加用户
     * @param user
     */
    @Insert("insert into user ( userName, password, email, createTime) values(#{userName},#{password},#{email},now())")
    @Options(useGeneratedKeys=true, keyProperty="userId", keyColumn="userId")
    int addUser(User user);

    @Insert("insert into user_role(userId,roleId) values(#{userId},#{roleId})")
    int insertUserRole(Integer userId,Integer roleId);

    /**
     * 根据ID 删除用户
     * @param userId
     */
    @Delete("delete from user where userId = #{userId}")
    int deleteUser(Integer userId);

    @Delete("delete from user_role where userId = #{userId}")
    int deleteUserRole(Integer userId);

    /**
     * 修改用户信息
     * @param user
     */
    @Update("update user set userName=#{userName},password=#{password},email=#{email},createTime=now() where userId=#{userId}")
    int updateUser(User user);

    /**
     * 模糊查询
     * @param str
     * @return
     */
//    @Select("select * from user where userName like concat('%',#{str},'%')")
    @Select("select u.*,r.roleId,r.roleName from user u, role r,user_role ur where u.userId = ur.userId and r.roleId = ur.roleId and userName like concat('%',#{str},'%') limit #{pageNum},#{pageSize}")
    List<UserRole> queryByPage(String str,Integer pageNum,Integer pageSize);

    @Select("select u.*,r.roleId,r.roleName from user u, role r,user_role ur where u.userId = ur.userId and r.roleId = ur.roleId and userName like concat('%',#{str},'%')")
    List<UserRole> query(String str);
}
